<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/user' }">用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体内容	 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="6">
          <el-input
            placeholder="请输入用户名"
            class="input-with-select"
            v-model="queryInfo.byname"
            clearable
            @clear="getUserList"
          >
            <el-button slot="append" icon="el-icon-search" @click="serarch"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addDialog">添加用户</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="userList" border style="width: 100%">
        <el-table-column type="index" width="100"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column label="头像" width="110" prop="head_img">
          <template slot-scope="scope">
            <img :src="scope.row.head_img" style="height: 60px" />
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" >
        </el-table-column>
        <el-table-column prop="email" label="邮箱" >
        </el-table-column>

        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="editUserlist(scope.row)"
              ></el-button>
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeUser(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        align="left"
        @size-change="SizeChange"
        @current-change="CurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加用户dialog对话框 -->

    <el-dialog
      align="left"
      title="添加用户"
      :visible.sync="addUserVisible"
      width="50%"
      @close="closeAdduserDialog"
    >
      <!--主体部分 -->
      <el-form
        :model="addUserForm"
        status-icon
        :rules="addUserFormRul"
        ref="addUserFormRel"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input
            v-model="addUserForm.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input
            type="password"
            v-model="addUserForm.pwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addUserForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model.number="addUserForm.phone"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addUserVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserList">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户diaolog对话框 -->

    <el-dialog align="left" title="修改用户" :visible.sync="editUserVisible" width="50%" >
      <!--主体部分 -->
      <el-form :model="editUserForm" status-icon  ref="editUserFormRel" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" >
          <el-input v-model="editUserForm.name"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" >
          <el-input v-model="editUserForm.pwd"  autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="邮箱" >
          <el-input v-model="editUserForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" >
          <el-input v-model.number="editUserForm.phone"></el-input>
        </el-form-item>
        <!-- 头像-->
        <el-form-item label="头像"  style="width: 300px">
          <el-upload
              action="http://localhost:8089/api/v1/pub/video/uploadImg"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              v-model="editUserForm.head_img"
              :limit=1
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
      		<el-button @click="editUserVisible = false">取 消</el-button>
      		<el-button type="primary" @click="editUser">确 定</el-button>
      	</span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    //验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/;
      if (regEmail.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的邮箱"));
    };
    //验证手机号码的规则
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^1[345789]\d{9}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的手机号码"));
    };
    return {
      //用户列表
      userList: [],
      head_img: [],
      queryInfo: {
        byname: "",
        pagenum: 1,
        pagesize: 4,
      },
      total: 0,
      //添加用户dialog,控制显示隐藏
      addUserVisible: false,
      //添加用户参数
      addUserForm: {
        name: "",
        pwd: "",
        email: "",
        phone: "",
      },
      //添加用户规则
      addUserFormRul: {
        name: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        pwd: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 1,
            max: 10,
            message: "长度为1-10个字符",
            trigger: "blur",
          },
        ],
        email: [{ validator: checkEmail, trigger: "blur" }],
        phone: [{ validator: checkMobile, trigger: "blur" }],
      },
      //修改用户参数
      editUserForm: {
        name: "",
        pwd:'',
        email: "",
        phone: "",
        id: "",
        head_img:''
      },
      //修改用户dialog,控制显示隐藏
      editUserVisible: false,
    };
  },
  mounted() {
    this.getUserList();
  },
  methods: {
    //请求用户列表
    getUserList() {
      this.$http
        .get("api/v1/pri/user/findUser", {
          params: this.queryInfo,
        })
        .then((res) => {
          console.log(res);
          if (res.status != 200) {
            return this.$message.error("获取用户列表失败");
          } else {
            this.$message.success("获取用户列表成功");
            this.userList = res.data.list;
            this.total = res.data.total;
            for (let i = 0; i < this.userList.length; i++) {
              this.head_img[i] = this.userList[i].head_img;
            }
          }
        });
    },
    //每页数据条数改变时触发
    SizeChange(newval) {
      this.queryInfo.pagesize = newval;
      this.getUserList();
    },
    //当前页面改变时触发
    CurrentChange(newval) {
      this.queryInfo.pagenum = newval;
      this.getUserList();
    },
    //查詢
    serarch(){
      this.getUserList()
    },
    //关闭添加用户对话框，重置表单
    closeAdduserDialog() {
      this.$refs.addUserFormRel.resetFields();
    },
    //点击添加新用户
    addDialog() {
      this.addUserVisible = !this.addUserVisible;
    },
    //点击确定发送添加新用户接口
    addUserList() {
      this.$refs.addUserFormRel.validate((valid) => {
        //校验规则全部正确向后台发送请求
        if (!valid) return;
        this.$http.post("api/v1/pri/user/addUser", this.addUserForm).then((res) => {
          console.log(res+"-------------------------------------");
          if (res.status !== 200)
            return this.$message.error("添加用户失败！");
          this.$message.success("添加用户成功！");
          //刷新列表
          this.getUserList();
          this.addUserVisible = !this.addUserVisible;
        });
      });
    },
    //删除用户
    removeUser(id) {
      this.$http.delete(`api/v1/pri/user/${id}`).then((res) => {
        console.log("========================================");
        console.log(res + "--------------------------------------");
        //刷新列表
        this.getUserList();
      });
    },
    //修改用户信息事件
    editUserlist(item){
      this.editUserVisible = !this.editUserVisible;
      this.editUserForm={
        name:item.name,
        email: item.email,
        phone: item.phone,
        head_img: item.head_img,
        pwd: item.pwd,
        id:item.id
      }
    },
    //点击确定发送请求修改用户信息
    editUser(){
      this.$http.put("api/v1/pri/user/edit",this.editUserForm).then(res=>{
        //console.log(res);
        if(res.data.code!=0) return this.$message.error("修改用户信息失败！");
        this.$message.success("修改用户信息成功！");
        this.getUserList();
        this.editUserVisible = !this.editUserVisible;
      })
    },
    //头像
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(response){
      this.editUserForm.head_img=response.data;
    },
  },
};
</script>

<style>
</style>